package com.authine.cloudpivot.web.api.secondopen.controller;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.web.api.secondopen.domain.AjaxResult;
import com.authine.cloudpivot.web.api.secondopen.modules.DysqParamDTO;
import com.authine.cloudpivot.web.api.secondopen.modules.DysqParamVO;
import com.authine.cloudpivot.web.api.secondopen.modules.PartInfoVO;
import com.authine.cloudpivot.web.api.secondopen.modules.QadResultVO;
import com.authine.cloudpivot.web.api.secondopen.service.ProductionManagementService;
import com.authine.cloudpivot.web.api.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 生产管理控制器
 */
@Api(value = "二开接口",tags = "生产管理控制器")
@RestController
@RequestMapping("/api/promanagement")
public class ProductionManagementController {
    private static final Logger log = LoggerFactory.getLogger(ProductionManagementController.class);
    @Autowired
    private ProductionManagementService productionManagementService;

    /**
     * 根据零件号获取零件信息
     * @return
     */
    @RequestMapping("/getPartInfo")
    public AjaxResult getPartInfo(@RequestParam(value = "parts") String parts){
        log.info("获取参数：parts={}",parts);
        List<PartInfoVO> partInfo = productionManagementService.getPartInfo(parts);
        if (CollectionUtils.isEmpty(partInfo)){
            return AjaxResult.error(300,"查询失败，列表为空");
        }
        return AjaxResult.success("查询成功",partInfo);
    }

//    /**
//     * 回传代用申请单数据给QAD
//     */
//    @RequestMapping("/returnDysqParams")
//    public AjaxResult returnDysqParams(@RequestBody DysqParamDTO dysqParamDTO){
//        log.info("获取参数：dysqParamDTO:"+JSON.toJSONString(dysqParamDTO));
//        //参数转换
//        DysqParamVO vos=new DysqParamVO();
//        vos.setMdmYpslPar(dysqParamDTO.getSpbm());
//        vos.setMdmYpslPart(dysqParamDTO.getPart());
//        vos.setMdmYpslSubPart(dysqParamDTO.getSubpart());
//        vos.setMdmYpslYrper(dysqParamDTO.getQj());
//        vos.setMdmYpslQtyLimit(dysqParamDTO.getXzsl());
//        vos.setMdmYpslNoLimit(dysqParamDTO.getSfxztdsl());
//        vos.setMdmYpslSubFirst(dysqParamDTO.getYxtd());
//        vos.setMdmYpslSubType(dysqParamDTO.getTdtype());
//        vos.setMdmYpslUser(dysqParamDTO.getUser());
//        vos.setMdmYpslRmks(dysqParamDTO.getRmks());
//
//        //获取操作方式
//        String czfs = dysqParamDTO.getCzfs();
//        QadResultVO qadResultVO = productionManagementService.sendDysqToMes(vos,czfs);
//        String code = qadResultVO.getCode();
//        String  msg = qadResultVO.getMsg();
//        Object data = qadResultVO.getData();
//        if (code.equals("10000")){
//            return  AjaxResult.success(msg,data);
//        }else {
//            return AjaxResult.error(msg,data);
//        }
//    }

    /**
     * 回传代用申请单数据给QAD,期间增加为多个,对象的期间变更为数组
     */
    @RequestMapping("/returnDysqParams")
    public AjaxResult returnDysqParams2(@RequestBody DysqParamDTO dysqParamDTO){
        log.info("获取参数：dysqParamDTO:"+JSON.toJSONString(dysqParamDTO));
        QadResultVO qadResultVO=new QadResultVO();
        String msg="";
        String succmsg="";
        //获取sa码
        String[] spbm = dysqParamDTO.getSpbm();
        //获取期间
        String[] qj = dysqParamDTO.getQj();
        //SA码为空
        if (StringUtils.isEmpty(spbm)){
            log.info("编码为空");
            for (int i = 0; i < qj.length; i++) {
                //参数转换
                DysqParamVO vos=new DysqParamVO();
                vos.setMdmYpslPar("");
                vos.setMdmYpslPart(dysqParamDTO.getPart());
                vos.setMdmYpslSubPart(dysqParamDTO.getSubpart());
                vos.setMdmYpslYrper(qj[i]);
                vos.setMdmYpslQtyLimit(dysqParamDTO.getXzsl());
                vos.setMdmYpslNoLimit(dysqParamDTO.getSfxztdsl());
                vos.setMdmYpslSubFirst(dysqParamDTO.getYxtd());
                vos.setMdmYpslSubType(dysqParamDTO.getTdtype());
                vos.setMdmYpslUser(dysqParamDTO.getUser());
                vos.setMdmYpslRmks(dysqParamDTO.getRmks());
                //获取操作方式
                String czfs = dysqParamDTO.getCzfs();
                log.info("传输的参数为{}",vos);
                qadResultVO = productionManagementService.sendDysqToMes(vos,czfs);
                String code = qadResultVO.getCode();
                String remsg = qadResultVO.getMsg();
                if ("10000".equals(code)){
                    succmsg=succmsg+"期间:"+qj[i]+"添加成功,";
                }
                if ("10001".equals(code)){
                    msg=msg+"期间:"+qj[i]+"失败，"+"失败原因："+remsg+",";
                }
            }
        }else{
            log.info("编码不为空");
            for (int j = 0; j < spbm.length; j++) {
                for (int i = 0; i < qj.length; i++) {
                    //参数转换
                    DysqParamVO vos=new DysqParamVO();
                    vos.setMdmYpslPar(spbm[j]);
                    vos.setMdmYpslPart(dysqParamDTO.getPart());
                    vos.setMdmYpslSubPart(dysqParamDTO.getSubpart());
                    vos.setMdmYpslYrper(qj[i]);
                    vos.setMdmYpslQtyLimit(dysqParamDTO.getXzsl());
                    vos.setMdmYpslNoLimit(dysqParamDTO.getSfxztdsl());
                    vos.setMdmYpslSubFirst(dysqParamDTO.getYxtd());
                    vos.setMdmYpslSubType(dysqParamDTO.getTdtype());
                    vos.setMdmYpslUser(dysqParamDTO.getUser());
                    vos.setMdmYpslRmks(dysqParamDTO.getRmks());
                    //获取操作方式
                    String czfs = dysqParamDTO.getCzfs();
                    log.info("传输的参数为{}",vos);
                    qadResultVO = productionManagementService.sendDysqToMes(vos,czfs);
                    String code = qadResultVO.getCode();
                    String remsg = qadResultVO.getMsg();
                    if ("10000".equals(code)){
                        succmsg=succmsg+"期间:"+qj[i]+"添加成功,";
                    }
                    if ("10001".equals(code)){
                        msg=msg+"期间:"+qj[i]+"失败，"+"失败原因："+remsg+",";
                    }
                }
            }
        }
        if (StringUtils.isEmpty(msg)){
            return  AjaxResult.success("操作成功");
        }else {
            return AjaxResult.error(succmsg+msg);
        }
    }


    /**
     * 根据CA码获取SA码
     */
    @RequestMapping("/getsabyca")
    public AjaxResult getSAByCA(String domain,String compfr,String compto){
        log.info("获取参数：domain={},compfr={},compto={}",domain,compfr,compto);
        List<PartInfoVO> saByCA = productionManagementService.getSAByCA(domain, compfr, compto);
        if (CollectionUtils.isEmpty(saByCA)){
            return AjaxResult.error(300,"查询失败，列表为空");
        }
        return AjaxResult.success("查询成功",saByCA);
    }
}
